#!/bin/bash

#运维神god周之：客机ssh服务初始化

#sudo passwd root
#{改密码}
#su root

#安装sshd服务，修改sshd_config配置文件
editFile(){
  yum -y install openssh-clients openssh-server openssh-askpass
  local file='sshd_config'
  local line1
  local line2
  local line3

  # /= 是打印行号的意思，如果是/p则打印匹配到的文本
  line1=$(sed -nr '/^[#]{0,1}LoginGraceTime/=' $file)
  sed -i "${line1}c LoginGraceTime 120" $file
  # 寻找被注释的配置项PermitRootLogin
  line2=$(sed -nr '/^[#]{0,1}PermitRootLogin without passwd/=' $file)
  sed -i "${line2}c PermitRootLogin yes" $file
  line3=$(sed -nr '/^[#]{0,1}StrictModes/=' $file)
  sed -i "${line3}c StrictModes yes" $file
  return 0
}

#开启ssh服务，修改自启动级别，开启22端口
serviceOn(){
  service sshd status
  service sshd start
  chkconfig --level 2345 sshd on
  firewall-cmd --zone=public --add-port=22/tcp --permanent
  firewall-cmd --reload
  return 0
}

editFile
serviceOn